﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class WarningList : Form
    {
        private PurchaseDao dao = null;
        public WarningList()
        {
            InitializeComponent();
            dao = new PurchaseDao();
            loadImage();
 
        }

        private Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

        private Image lImage(string url)
        {
            try
            {
                Image image = Image.FromFile(url);
                Image imagedis = resizeImage(image, new Size(150, 150));
                image.Dispose();
                return imagedis;
            }
            catch
            {
                try
                {
                    Image image = Image.FromFile(@"Resources\\NoImage.png");
                    Image imagedis = resizeImage(image, new Size(150, 150));
                    image.Dispose();
                    return imagedis;
                }
                catch
                {
                    return null;
                }
            }
        }

        private DataGridViewImageCell loadImage(string url)
        {
            try
            {
                Image image = Image.FromFile(url);
                DataGridViewImageCell imageCell = new DataGridViewImageCell();
                Image imagedis = resizeImage(image, new Size(150, 150));
                imageCell.Value = imagedis;
                image.Dispose();
                return imageCell;
            }
            catch
            {
                return new DataGridViewImageCell();
            }
        }
        private void loadImage()
        {
            lvCustomers.Items.Clear();
            DataTable table = dao.getCustommerWarning(txtName.Text.Trim());
              int index = 0;
              if (table != null && table.Rows.Count > 0)
              {
                  foreach (DataRow row in table.Rows)
                  {
                      ListViewItem item = new ListViewItem(row["NAME"].ToString());
                      item.ImageIndex = index;
                      if (row["PICTURE"] != null)
                      {
                          Image image = lImage(row["PICTURE"].ToString());
                          if (image != null)
                          {
                              imlWarning.Images.Add(image);
                          }
                      }

                      index++;
                      lvCustomers.Items.Add(item);
                      item.SubItems.Add(row["ID"].ToString());
                  }
                  imlWarning.ImageSize = new Size(150, 150);
                  lvCustomers.LargeImageList = imlWarning;

                  txtResult.Text = table.Rows.Count + " học viên.";
              }
              else
              {
                  txtResult.Text = "";
              }
        }

        private void lvCustomers_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
           
        }

        private void lvCustomers_SelectedIndexChanged(object sender, EventArgs e)
        {
            

        }

        private void lvCustomers_ItemActivate(object sender, EventArgs e)
        {
            
        }

        private void lvCustomers_DoubleClick(object sender, EventArgs e)
        {
            ListViewItem item = lvCustomers.SelectedItems[0];
            int id = 0;
            int.TryParse(item.SubItems[1].Text.ToString().TrimEnd(), out id);
            Purchase p = new Purchase(id);
            p.ShowDialog();
            loadImage();
        }

        private void lvCustomers_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            loadImage();
        }

        private void btnWarning_Click(object sender, EventArgs e)
        {
            CustomerList f = new CustomerList();
            f.ShowDialog();
            f.Dispose();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Purchase p = new Purchase(true);
            p.ShowDialog();
            p.Dispose();
        }

        private void btnAddCustomerDay_Click(object sender, EventArgs e)
        {
            
            PurchaseDay p = new PurchaseDay(true);
            p.ShowDialog();
            p.Dispose();
        }
    }
}
